/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package xmlutils;

import DTO.song.Artist;
import DTO.song.Artists;
import DTO.song.Genre;
import DTO.song.Likers;
import DTO.song.Song;
import DTO.song.Songs;
import java.math.BigInteger;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 *
 * @author NoiR
 */
public class SearchSongSAXHandler extends DefaultHandler {

    private Song song;
    private Songs songs;
    private Artist artist;
    private Artists artists;
    private Genre genre;
    private boolean isFound;
    private String tag;
    private Likers likers;
    private String id;

    public SearchSongSAXHandler() {
        this.isFound = false;
        this.tag = "";
        this.songs = new Songs();
    }

    public SearchSongSAXHandler(String id) {
        this.isFound = false;
        this.tag = "";
        this.id = id;
        this.songs = new Songs();
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        tag = qName;
        if (tag.equals("Song")) {
            this.song = new Song();
        }

        if (isFound) {
            if (tag.equals("Artists")) {
                this.artists = new Artists();
            } else if (tag.equals("Artist")) {
                this.artist = new Artist();
            } else if (tag.equals("Genre")) {
                this.genre = new Genre();
            } else if (tag.equals("Likers")) {
                this.likers = new Likers();
            }
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        tag = qName;


        if (isFound) {
            if (tag.equals("Songs")) {
                this.songs.getSong().add(song);
            } else if (tag.equals("Song")) {
                this.songs.getSong().add(song);
                this.isFound = false;
            } else if (tag.equals("Artists")) {
                this.song.setArtists(artists);
            } else if (tag.equals("Artist")) {
                this.artists.getArtist().add(artist);
            } else if (tag.equals("Genre")) {
                this.song.setGenre(genre);
            } else if (tag.equals("Likers")) {
                this.song.setLikers(likers);
            }
        }
        tag = "";


    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        String str = new String(ch, start, length);

        if ("SongID".equals(tag)) {
            if (str.trim().equals(id)) {
                isFound = true;
                //song.setSongID(BigInteger.valueOf(Integer.parseInt(str.trim())));
                this.song.setSongID(BigInteger.valueOf(Integer.parseInt(str.trim())));
            }
        }
        if (isFound) {
            if ("SongTitle".equals(tag)) {
                this.song.setSongTitle(str.trim());


            } else if ("SongViews".equals(tag)) {
                this.song.setSongViews(BigInteger.valueOf(Integer.parseInt(str.trim())));


            } else if ("SongSRC".equals(tag)) {
                this.song.setSongSRC(str.trim());


            } else if ("Lyrics".equals(tag)) {
                this.song.setLyrics(str.trim());


            } else if ("ArtistID".equals(tag)) {
                this.artist.setArtistID(BigInteger.valueOf(Integer.parseInt(str.trim())));


            } else if ("ArtistName".equals(tag)) {
                this.artist.setArtistName(str.trim());


            } else if ("GenreID".equals(tag)) {
                this.genre.setGenreID(BigInteger.valueOf(Integer.parseInt(str.trim())));


            } else if ("GenreName".equals(tag)) {
                this.genre.setGenreName(str.trim());


            } else if ("AlbumID".equals(tag)) {
                this.song.setAlbumID(BigInteger.valueOf(Integer.parseInt(str.trim())));


            } else if ("AlbumTitle".equals(tag)) {
                this.song.setAlbumTitle(str.trim());


            } else if ("isMV".equals(tag)) {
                this.song.setIsMV(Boolean.parseBoolean(str));


            }
        }
    }

    public Songs getSongs() {
        return this.songs;

    }
}
